package com.qinghe.pro.controller.file;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.qinghe.pro.Constants;
import com.qinghe.utils.FileUtils;
import com.qinghe.utils.Utils;

@Controller
@RequestMapping("/upload")
public class UploadController {

	private static final String STORE_DIR = Constants.getXheditorPath();
	private static String[] LINK_EXTS = {"zip", "rar", "txt", "doc", "docx"};
	private static String[] IMG_EXTS = {"jpg", "jpeg", "gif", "png"};
	private static String[] FLASH_EXTS = {"swf"};
	private static String[] MEDIA_EXTS = {"avi", "rm", "rmvb", "mkv", "qmv", "mp4"};
	private static final int LINK_SIZE = 1024 * 1024 * 5;
	private static final int IMG_SIZE = 1024 * 1024 * 2;
	private static final int FLASH_SIZE = 1024 * 1024 * 5;
	private static final int MEDIA_SIZE = 1024 * 1024 * 10;
	
	/**
	 * xheditor附件上传
	 * @param request HttpServletRequest
	 * @param filedata MultipartFile
	 * @param flag link|img|flash|media
	 * @param uuid 唯一码（用做附件文件夹名）
	 * @param immediate 1：立即上传模式
	 */
	@RequestMapping(value = "/xheditor", method = RequestMethod.POST)
	public @ResponseBody String xheditor(HttpServletRequest request,
			@RequestParam MultipartFile filedata, @RequestParam String flag,
			@RequestParam String uuid, @RequestParam Integer immediate) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("msg", "");
		map.put("err", "");
		
		try {
			String fileExt = FileUtils.getFileExt(filedata.getOriginalFilename());
			
			if ("link".equals(flag)) {
				if (!ArrayUtils.contains(LINK_EXTS, fileExt)) {
					map.put("err", "链接地址文件的扩展名必须为：" + StringUtils.join(LINK_EXTS, ","));
					return Utils.JsonUtil.toJson(map);
				}
				if (filedata.getSize() > LINK_SIZE) {
					map.put("err", "链接地址文件的大小限制为：" + LINK_SIZE/1024/1024 + "M");
					return Utils.JsonUtil.toJson(map);
				}
			} else if ("img".equals(flag)) {
				if (!ArrayUtils.contains(IMG_EXTS, fileExt)) {
					map.put("err", "图片文件的扩展名必须为：" + StringUtils.join(IMG_EXTS, ","));
					return Utils.JsonUtil.toJson(map);
				}
				if (filedata.getSize() > IMG_SIZE) {
					map.put("err", "图片文件的大小限制为：" + IMG_SIZE/1024/1024 + "M");
					return Utils.JsonUtil.toJson(map);
				}
			} else if ("flash".equals(flag)) {
				if (!ArrayUtils.contains(FLASH_EXTS, fileExt)) {
					map.put("err", "动画文件的扩展名必须为：" + StringUtils.join(FLASH_EXTS, ","));
					return Utils.JsonUtil.toJson(map);
				}
				if (filedata.getSize() > FLASH_SIZE) {
					map.put("err", "动画文件的大小限制为：" + FLASH_SIZE/1024/1024 + "M");
					return Utils.JsonUtil.toJson(map);
				}
			} else if ("media".equals(flag)) {
				if (!ArrayUtils.contains(MEDIA_EXTS, fileExt)) {
					map.put("err", "媒体文件的扩展名必须为：" + StringUtils.join(MEDIA_EXTS, ","));
					return Utils.JsonUtil.toJson(map);
				}
				if (filedata.getSize() > MEDIA_SIZE) {
					map.put("err", "媒体文件的大小限制为：" + MEDIA_SIZE/1024/1024 + "M");
					return Utils.JsonUtil.toJson(map);
				}
			}
			
			// 要保存的物理文件名 + 后缀名
			Date now = new Date();
			String newFileName = flag + Utils.DateUtil.format("yyyyMMddHHmmssSSSS", now) + "." + fileExt;
			
			String uploadPath = FileUtils.getFileStorePath(request) + STORE_DIR + File.separator + uuid;
			
			// 构建文件要保存到的目录
			File uploadedFile = new File(uploadPath, newFileName);
			
			org.apache.commons.io.FileUtils.writeByteArrayToFile(uploadedFile, filedata.getBytes());
			
			String msg = FileUtils.getContextPath(request) + STORE_DIR + "/" + uuid + "/" + newFileName;
			// 立即上传模式
			if (immediate == 1) {
				msg = "!" + msg;
			}
			map.put("msg", msg);
		} catch (Exception e) {
			map.put("err", e.getMessage());
		}
		
		return Utils.JsonUtil.toJson(map);
	}

}
